import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class MapManager extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	private MapEditor mapEditorPanel = null;
	private MenuBar menubar = null;
	private Menu mn1, mn2;
	private MenuItem mi1_1, mi1_2, mi1_3, mi1_4,  mi2_1, mi2_2, mi2_3;

	//フォントの設定
	private Font font1 = new Font("Tahoma", Font.PLAIN, 12 );

	private void initialize() {
        this.setSize( new Dimension(
        		getMapEditorPanel().getMapPanel().getImgWidth()+250,
        		getMapEditorPanel().getMapPanel().getImgHeight()+130));
        //地図画像サイズの大きさ+操作パネル＋フレーム
        this.setMenuBar(getMyMenuBar());
	}

	private MenuBar getMyMenuBar(){
		if(menubar==null){
	        menubar = new MenuBar();

	        mn1 = new Menu("FILE");
	        menubar.setFont(font1);
	        menubar.add(mn1);
	        mi1_1 = new MenuItem("Open");
	        mi1_2 = new MenuItem("Save");
	        mi1_3 = new MenuItem("Exit");
	        mi1_4 = new MenuItem("Write RouteMap");
	        mn1.add(mi1_1);
	        mn1.add(mi1_2);
	        mn1.addSeparator();
	        mn1.add(mi1_4);
	        mn1.addSeparator();
	        mn1.add(mi1_3);
	        mi1_1.addActionListener(this);
	        mi1_2.addActionListener(this);
	        mi1_3.addActionListener(this);
	        mi1_4.addActionListener(this);
	        mn2 = new Menu("MODE");
	        menubar.add(mn2);
	        mi2_1 = new MenuItem("Edit Map");
	        mi2_2 = new MenuItem("Dispaly Status");
	        mi2_3 = new MenuItem("Control Robot");
	        mn2.add(mi2_1);
	        mn2.add(mi2_2);
	        mn2.add(mi2_3);
	        mi2_2.setEnabled(false);
	        mi2_3.setEnabled(false);
		}
		return menubar;
	}

	 public void actionPerformed (ActionEvent e) {
		    Object obj = e.getSource();
		    if (obj == mi1_1){
		    	JFileChooser fc_read = new JFileChooser("data/");
		    	int selected_read = fc_read.showOpenDialog(this);
		    	if(selected_read == JFileChooser.APPROVE_OPTION ){
		    		      File file_read = fc_read.getSelectedFile();
		    		      mapEditorPanel.getMapPanel().parsePathDataFile(file_read);
		    	}
		    }
		    else if (obj == mi1_2){
		    	JFileChooser fc_write = new JFileChooser("data/");
		    	int selected_write = fc_write.showSaveDialog(this);
		    	if( selected_write == JFileChooser.APPROVE_OPTION){
		    		File file_write = fc_write.getSelectedFile();
		    		mapEditorPanel.getMapPanel().writePathDataFile(file_write, false);
		    		}
		    }
		    else if (obj == mi1_3)
		    	System.exit(0);
		    else if (obj == mi1_4){ //PathPlanningモジュールで用いるroutemap形式のファイルを書き出す．
		    	JFileChooser fc_write = new JFileChooser("data/");
		    	int selected_write = fc_write.showSaveDialog(this);
		    	if( selected_write == JFileChooser.APPROVE_OPTION){
		    		File file_write = fc_write.getSelectedFile();
		    		mapEditorPanel.getMapPanel().writePathDataFile(file_write, true);
		    		}
		    }
		  }

	private MapEditor getMapEditorPanel() {
		if (mapEditorPanel == null) {
			mapEditorPanel = new MapEditor();
		}
		return mapEditorPanel;
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MapManager thisClass = new MapManager();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	public MapManager() {
		super();
		this.setContentPane(getMapEditorPanel());
		initialize();
		this.setTitle("MapManager");
		this.setResizable(true);
	}

}
